/* 公用弹层alertTip */
function alertTipDown(message, className) {
	$('.alerTipDl dt').removeClass().addClass(className);
	$('.alerTipDl dd').html(message);
	$('.alerTip').stop(true).animate({
				'top' : '-5px'
			});
	alertTipUp();
}
function alertTipUp() {
	function tipUp() {
		var top = $('.alerTip').height() + 50;
		$('.alerTip').stop(true).animate({
					'top' : -top + 'px'
				});
	}
	var timer = setTimeout(tipUp, 2000);
	$('.alerTip').hover(function() {
				clearTimeout(timer);
				$('.alerTipP a').click(function() {
							tipUp();
						});
			}, function() {
				timer = setTimeout(tipUp, 2000);
			});
}
// 头部消息弹窗
function headnotification() {
	$('#header-notification').click(function() {
				$('#header-notification').addClass('header-notification-focus');
				$('.header-notification-pop').show();
			});
	$(document).mousedown(function(event) {
		if ($(event.target).hasClass('.header-notification-pop')) {
			return;
		} else if ($(event.target).parents('.header-notification-pop').length <= 0) {
			$('.header-notification-pop').hide();
			$('#header-notification').removeClass('header-notification-focus');
		};
	});
}
// 头部设置弹窗
function headSetting() {
	$('#header-user').hover(function() {
				$('#header-setnav').show();
			}, function() {
				$('#header-setnav').hide();
			});
}
// 公用弹层框架
function popPub(clickIt, clickItShow, shut) {
	clickIt.click(function() {
				popPosition(clickItShow);
				clickItShow.show();
				appendCover();
			});
	shut.click(function() {
				clickItShow.hide();
				removeCover();
				$(".logPop input").each(function(){
					$(this).val(this.defaultValue).next('span').show();
				});
			});
};
// 公用弹层cover
function appendCover() {
	$('body').append('<div id="cover"></div>');
	$('#cover').css({
				'height' : $(document).height()
			});
};
function removeCover() {
	$('#cover').remove();
};
// 公用弹层定位
function popPosition(obj) {
	obj.css({
				'top' : $(window).scrollTop() + 50
			});
};
// 发ask添加图片
function addAskImg() {
	$('.ask_pop_txt').keyup(function() {
				if ($.trim($(this).val()).length > 0) {
					$('.ask_pop_addimgtit ').addClass('ask_pop_addimgtitfocus');
				} else {
					$('.ask_pop_addimgtit ')
							.removeClass('ask_pop_addimgtitfocus');
				}
			});
	$('.ask_pop_txt').keyup();
	$('.ask_pop_addimgtit').click(function() {
		var pop_searchimg = $('.ask_pop_searchimg');
		defaultvalue(pop_searchimg);
		pop_searchimg.focus();
		if ($(this).hasClass('ask_pop_addimgtitfocus')) {
			$('.ask_pop_addimg').slideDown();
		}
		var askImageTimeOut ;
		$(".ask_pop_searchimg").keyup(function() {
					clearTimeout(askImageTimeOut);
					askImageTimeOut = setTimeout(askImageInit,750);
					$("#c1-grid .c1-page-prev").unbind().click(askImagePrevPage);
					$("#c1-grid .c1-page-next").unbind().click(askImageNextPage);
				});
		askImageInit();
		$(".upfile").change(uploadPicture);
		$(".ask_pop_yesbtn_no").click(createAsk);
	});
}
// 输入框defaultvalue隐藏
function txtDefaultvalue(txtobj, txtDefaultvalue) {
	txtobj.focus(function() {
				txtDefaultvalue.hide();
			});
	txtobj.blur(function() {
				if ($.trim(txtobj.val()).length <= 0) {
					txtDefaultvalue.show();
				}
			});
	txtDefaultvalue.click(function() {
				$(this).hide();
				txtobj.focus();
			});
//	txtobj.focus();
};
// 文本框默认值
function textSize(txtObj, curObj, maxObj) {
	var curSize = $.trim(txtObj.val()).length, maxSize = parseInt($.trim(maxObj
			.text()));
	txtObj.keyup(function() {
				curSize = $.trim(txtObj.val()).length;
				if (curSize > maxSize) {
					txtObj.val($.trim(txtObj.val()).substr(0, maxSize));
				}
				curSize = $.trim(txtObj.val()).length;
				curObj.text(curSize);
			});
	txtObj.keyup();
}
/** 文本框的默认值defaultvalue* */
function defaultvalue(obj) {
	obj.focus(function() {
				$(this).addClass('focus');
				if ($(this).val() == this.defaultValue) {
					$(this).val('');
				}
			}).blur(function() {
				$(this).removeClass('focus');
				if (this.value == '') {
					$(this).val(this.defaultValue);
				}
			});
};
// 输入框选中状态
function txtFocus(obj) {
	obj.focus(function() {
				obj.addClass('txtFocus');
			});
	obj.blur(function() {
				obj.removeClass('txtFocus');
			});
}

function doAjax(url, params, funSuccess) {
	$.ajax({
				type : "POST",
				dataType : "json",
				url : url,
				cache : false,
				data : params,
				beforeSend : disAjaxLoading,
				success : funSuccess,
				complete : hideAjaxLoading,
				clearForm : true
			});
}
function doAsyncAjax(url, params, funSuccess) {
	$.ajax({
				type : "POST",
				dataType : "json",
				url : url,
				cache : false,
				async : false,
				data : params,
				beforeSend : disAjaxLoading,
				success : funSuccess,
				complete : hideAjaxLoading,
				clearForm : true
			});
}
function doAsyncAjaxHtml(url, params, funSuccess) {
	$.ajax({
				type : "POST",
				dataType : "html",
				url : url,
				cache : false,
				async : false,
				data : params,
				beforeSend : disAjaxLoading,
				success : funSuccess,
				complete : hideAjaxLoading,
				clearForm : true
			});
}
function doAjaxHtml(url, params, funSuccess) {
	$.ajax({
				type : "POST",
				dataType : "html",
				url : url,
				cache : false,
				data : params,
				beforeSend : disAjaxLoading,
				success : funSuccess,
				complete : hideAjaxLoading,
				clearForm : true
			});
}

function disAjaxLoading() {
	var request = this.url;
	$('#askLoading').show();
}
function hideAjaxLoading() {
	var request = this.url;
	$('#askLoading').hide();
}
function hideAjaxLoadingError() {
	var request = this.url;
	$('#askLoading').hide();
}
function getAttrs(objs, attrName) {
	var attrs = new Array();
	objs.each(function(index, obj) {
				attrs.push($(obj).attr(attrName));
			});
	return attrs;
}

function contains(arr, obj) {
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == obj) {
			return true;
		}
	}
	return false;
}

function isUndefined(obj) {
	if (obj == null || obj == "" || obj == "undefined" || obj == undefined
			|| obj == 0 || obj == "0") {
		return true;
	}
	return false;
}

function toUserAskPage() {
	var url = "../user/toUserAsk.action";
	var loginName=$(".header-username").html();
	var params = {"loginName":loginName};
	document.location = "../user/toUserAsk.action?loginName="+loginName;
}
function loginWith(obj){
	var url = "../loginWith/loginWith.action";
	var params = {"type":$(obj).attr("type")};
	doAjax(url,params,function(ajaxResult){
		if(ajaxResult.OK){
			var myWindow=window.open(ajaxResult.resultMap.oauthUrl,"TencentLogin", "width=450,height=320,menubar=0,scrollbars=0,resizable=no,status=no,titlebar=0,toolbar=0,location=no");
			if(myWindow!=null){
				setInterval(function p(){
					if(myWindow.closed){
						window.location.href = "http://yoasking.com";
					}
				},1000);
			}
		}
	});
}